<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Automobiles extends CI_Controller {

	function __construct() {
		parent::__construct();
		//check session
		$this->session->set_userdata(array('redirectUrl' =>current_url()));
		check_user_login();
	}
	
	// load index file
	function index() {
		$this->_display((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
	}
	//get all the classifieds ads
	function view()
	{
		$crud = new grocery_CRUD();
		$crud->where('advertiserID',(int) $this->session->userdata('userid'));
		$crud->where('is_active', (int)1);
		$crud->set_table('automobile');
		$crud->display_as('brandID', 'Brand');
		$crud->display_as('typeID', 'Type');
		$crud->display_as('categoryID', 'You want to');

		$crud->set_relation('advertiserID','masteruser','firstName');
		$crud->set_relation('brandID','automobile_brand','brand_name');
		$crud->set_relation('typeID','automobile_type','auto_type');

		$crud->columns('title','description','posted_date', 'expired_date') ;
		//$crud->callback_edit_field('posted_date',function($value= ''){
		  //return $value;
		//});
		$crud->change_field_type('firstName', 'readonly');

		$crud->field_type('categoryID','dropdown',
            array(1 => 'Sell', 2 => 'Buy',3 => 'Rent'));
		$crud->field_type('price_denote','dropdown',
            array('KS' => 'KS', 'LAKH'=> 'LAKH','USD' => 'USD' , 'SGD' => 'SGD', 'CNY' => 'CNY'));
		$crud->field_type('is_negotiable','dropdown',
            array(0 => 'No', 1 => 'Yes'));
		$crud->field_type('engine_size','dropdown',
            array('GS' => 'Gas', 'OL'=> 'Oil','CNG' => 'CNG' , 'HBD' => 'Hybrid'));
		
		$crud->set_subject('Automobiles');
		/**
		$crud_columns('','','','') to set the number of columns display on list page
		$crud_fields('','','','') to set the number of columns display on details page
		$crud->display_as('lastName','Last Name'); to set display name
		**/
		
		$crud->edit_fields('categoryID','brandID','typeID','title','description','is_negotiable',
			'price','price_denote','model','condition', 'mileage','regyear','fuel_type','seats','engine_size','contact_person','contact_number','contact_email','primary_image','small_image1','small_image2','small_image3');
		$crud->set_field_upload('primary_image','assets/uploads/automobiles');
		$crud->set_field_upload('small_image1','assets/uploads/automobiles');
		$crud->set_field_upload('small_image2','assets/uploads/automobiles');
		$crud->set_field_upload('small_image3','assets/uploads/automobiles');

		$crud->unset_add();
		
		$output = $crud->render();
		$crud->unset_jquery_ui();
		$this->_display($output);
	}
	//private function output
	function _display($output = null)
	{
		
		$this->load->view('admin/automobiles/index.php',$output );	
	}

	//post new advertisement
	function post(){
		
		//$this->_set_fields();

		// set common properties
		$data['title'] = 'Post automobile ads';
		$data['message'] = '';
		$this->load->model('Autobrand_model','',TRUE);
		$this->load->model('Autotype_model','',TRUE);
		$this->load->model('User_model','',TRUE);
		
		$data['brands'] = $this->Autobrand_model->get_brands();
		$data['types'] = $this->Autotype_model->get_types();
		$userID = (int) $this->session->userdata('userid');
		$data['user'] = $this->User_model->get_by_id($userID);
		
		$this->load->helper('common_functions');
		$current_date = date('Y-m-d H:m:s');
		$data['expired_date'] = calculate_expiry_date($current_date, AUTO_RETENSION);
		
		$data['action'] = site_url('admin/automobiles/add');
		$this->load->view('admin/automobiles/form', $data);

	
	}
	//function add new adv
	function add(){
		
		// set common properties
		$data['title'] = 'Post automobile ads';
		//$this->_set_rules();

		// run form_validation
		if ($this->form_validation->run('automobilefrm') == FALSE){
			$data['message'] = 'You have missed out to fill the following field';
			$data['action'] = site_url('admin/automobiles/add');
			$data['user'] = null;
			$data['expired_date']  = $this->input->post('expired_date');
			$this->load->model('Autotype_model','',TRUE);
			$data['types'] = $this->Autotype_model->get_types();
			$data['selectedType'] = $this->input->post('types');
			$this->load->view('admin/automobiles/form', $data);
		}else{
			// save data
			$automobile = array('title' => $this->input->post('autotitle'),
					'contact_person' => $this->input->post('contact_person'),
					'contact_email' => $this->input->post('contact_email'),
					'contact_no' => $this->input->post('contact_no')
					);
			//vehicle's features/informations
			if($this->input->post('autoprice'))
				$automobile['price'] = $this->input->post('autoprice');
			if($this->input->post('price_denote'))
				$automobile['price_denote'] = $this->input->post('price_denote');
			if($this->input->post('model'))
				$automobile['model'] = $this->input->post('model');
			if($this->input->post('adescriptions'))
				$automobile['description'] = $this->input->post('adescriptions');
			if($this->input->post('selectType'))
				$automobile['typeID'] = $this->input->post('selectType');
			if($this->input->post('brand'))
				$automobile['brandID'] = $this->input->post('brand');
			if($this->input->post('condition'))
				$automobile['condition'] = $this->input->post('condition');
			if($this->input->post('mileage'))
				$automobile['mileage'] = $this->input->post('mileage');
			if($this->input->post('regyear'))
				$automobile['regyear'] = $this->input->post('regyear');
			if($this->input->post('fuel_type'))
				$automobile['fuel_type'] = $this->input->post('fuel_type');
			if($this->input->post('seats'))
				$automobile['seats'] = $this->input->post('seats');
			if($this->input->post('engsize'))
				$automobile['engine_size'] = $this->input->post('engsize');

			$adv['is_negotiable'] = $this->input->post('isnegotaible',TRUE)==null ? 0 : 1;
					
			$automobile['advertiserID']=$this->session->userdata('userid');
			if (is_uploaded_file($_FILES['userfile']['tmp_name'][0])){

				$image_url = upload_files(250,200,'/automobiles');
				//is_array ($image_url)/
			
				if($image_url){
					$urls = array_values($image_url);
					log_message('INFO', 'inside image url array' . $urls[0]);
					$automobile['primary_image'] = isset($urls[0]) ? $urls[0] : null;
					$automobile['small_image1'] = isset($urls[1]) ? $urls[1] : null;
					$automobile['small_image2'] = isset($urls[2]) ? $urls[2] : null;
					$automobile['small_image3'] = isset($urls[3]) ? $urls[3] : null;
					
				}else{
					$data = array('message' => 'upload problem' . $image_url, 'action' => 'admin/profile/add');
					$data['action'] = site_url('admin/automobiles/add');
					$data['user'] = null;
					$data['expired_date']  = $this->input->post('expired_date');
					$this->load->model('Autotype_model','',TRUE);
					$data['types'] = $this->Autotype_model->get_types();
					$data['selectedType'] = $this->input->post('types');
					$this->load->view('admin/automobiles/form', $data);
				}
			}
			$this->load->model('Automobile_model','',TRUE);
			$id = $this->Automobile_model->save($automobile);
				
			/**if ($id){
				$this->load->model('Credit_model','',TRUE);
				$result = $this->Credit_model->add($id,100);
			}**/
			
			// set form input name="id"
			$this->form_validation->id = $id;
				
			// set user message
			$data['message'] = '<div class="success">Congruations! Welcome to MayKha Family. Please check your email to activate the account and instantly enjoy $100 coins !!!</div>';
			redirect('admin/automobiles/view', $data);
		}

		//load view
	}
	
	
	

}

?>
